package com.example.news_demo.controller;

import com.example.news_demo.entity.Article;
import com.example.news_demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/TitleView")
public class SearchController {

    @Autowired
    ArticleService articleService;

    @RequestMapping("/search")
    public String getArticleListByWord(Model model, HttpServletRequest request){
        String word = request.getParameter("word");
        List<Article> articleList = articleService.getArticleListByWord(word);
        List<Article> randomList = articleService.getRandom(10);
        model.addAttribute("article",articleList);
        model.addAttribute("randomList",randomList);
        return "index";
    }

    @RequestMapping("/searchByLike")
    public String getArticleListByLike(Model model, HttpServletRequest request){
        String word = request.getParameter("word");
        List<Article> articleList = articleService.getArticleListByWord(word);
        List<Article> randomList = articleService.getRandom(10);
        model.addAttribute("article",articleList);
        model.addAttribute("randomList",randomList);
        return "index";
    }
}
